<?php

namespace app\common\model;

use think\Model;

/**
 * 审批流待审批用户
 */
class ApprovesWaitUser extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'remark',
        'status_text',
    ];


    public function getRemarkAttr($value,$data)
    {
        $user_id = $value ? $value : (isset($data['user_id']) ? $data['user_id'] : '');
        $approves_id = $value ? $value : (isset($data['approves_id']) ? $data['approves_id'] : '');
        $action = ApprovesAction::where(['approves_id'=>$approves_id,'oper_id'=>$user_id])->find();
        return !$action ? '' : $action->remark;
    }

    //关联用户
    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->field('id,UID,nickname,avatar');
    }

    //审批状态:cancel=已撤销,wait=待审批,ing=审批中,pass=审批通过,refuse=审批拒绝
    //状态
    public function getStatusLists()
    {
        return [
            '-1'        => '审批不通过',
            '0'         => '待审批',
            '1'         => '审批通过',
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        if($value === ''){
            return '';
        }
        return $this->getStatusLists()[$value];
    }

}
